<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline>
          <el-form-item>
            <el-button
              icon="el-icon-plus"
              type="success"
              @click="deBounceWrap(handleAdd)"
            >新增
            </el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="data" border>
        <el-table-column
          label="索引编号"
          prop="id"
          width="150px"
        ></el-table-column>

        <el-table-column label="列表排序" prop="sort"></el-table-column>
        <el-table-column label="充值回声币" prop="qipao_coin"></el-table-column>
        <el-table-column label="充值人民币" prop="rmb"></el-table-column>

        <el-table-column label="显示状态" prop="status" width="100px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="2"
              active-color="#13ce66"
              active-text="显示"
              class="switchStyle"
              inactive-color="#ff4949"
              inactive-text="隐藏"
              @change="getSwitch(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              plain
              size="mini"
              type="primary"
              @click="deBounceWrap(handleEdit, scope.row)"
            >编辑
            </el-button
            >
            <el-button
              icon="el-icon-delete"
              plain
              size="mini"
              type="danger"
              @click="deBounceWrap(handleDel, scope.row)"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- <cus-pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      /> -->
    </el-card>
    <el-dialog
      :before-close="handleClose"
      :close-on-click-modal="false"
      :title="titleOptions[title]"
      :visible.sync="dialogVisible"
      width="40%"
      @close="handleClear"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="列表排序" label-width="120px" prop="sort">
          <el-input
            v-model.trim="form.sort"
            clearable
            placeholder="请填写列表排序"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="充值回声币" label-width="120px" prop="qipao_coin">
          <el-input
            v-model.trim="form.qipao_coin"
            clearable
            placeholder="请填写充值回声币"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="充值人民币" label-width="120px" prop="rmb">
          <el-input
            v-model.trim="form.rmb"
            clearable
            placeholder="请填写充值人民币"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px" prop="status">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
            active-color="#13ce66"
            active-text="显示"
            class="switchStyle"
            inactive-color="#ff4949"
            inactive-text="隐藏"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button plain type="primary" @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
        >提交
        </el-button
        >
      </div>
    </el-dialog>

    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""
      ><em style="font-style:normal;color:red">删除后不可恢复</em
      >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addList, delList, editList, toGetList } from '@/api/operationManagement/rechargeAmountConfiguration'

export default {
  data() {
    return {
      data: [],
      listQuery: {
        client_type: 1
        // page: 1,
        // page_size: 10
      },
      // total: 0,

      titleOptions: {
        add: '新增',
        edit: '编辑'
      },
      title: 'add',
      rowData: {},
      form: {
        client_type: 1,
        sort: undefined,
        rmb: undefined,
        qipao_coin: undefined,
        status: undefined
      },
      rules: {
        sort: [{ required: true, message: '请输入列表排序', trigger: 'blur' }],
        rmb: [{ required: true, message: '请输入充值人民币', trigger: 'blur' }],
        qipao_coin: [
          { required: true, message: '请输入充值回声币', trigger: 'blur' }
        ],

        status: [{ required: true, message: '选择状态', trigger: 'change' }]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      dataIds: undefined
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //列表
    getList() {
      toGetList(this.listQuery).then(res => {
        this.data = res.data
      })
    },
    getSwitch(row) {
      let data = {
        id: row.id,
        sort: row.sort,
        rmb: row.rmb,
        qipao_coin: row.qipao_coin,
        status: row.status
      }
      editList(data).then(res => {
        if (res.code === 200) {
          this.$message.success('修改成功')
          this.getList()
        }
      })
    },

    //新增
    handleAdd() {
      this.title = 'add'
      this.dialogVisible = true
    },

    //编辑
    handleEdit(row) {
      this.title = 'edit'
      this.rowData = row
      this.form.sort = this.rowData.sort
      this.form.qipao_coin = this.rowData.qipao_coin
      this.form.rmb = this.rowData.rmb
      this.form.status = this.rowData.status
      this.dialogVisible = true
    },
    handleSumbit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title === 'add') {
            let data = {
              sort: this.form.sort,
              qipao_coin: this.form.qipao_coin,
              rmb: this.form.rmb,
              status: this.form.status,
              client_type: 1
            }
            addList(data).then(res => {
              if (res.code === 200) {
                this.$message.success('提交成功')
                this.getList()
                this.dialogVisible = false
              } else {
              }
            })
          }
          if (this.title === 'edit') {
            let data = {
              id: this.rowData.id,
              sort: this.form.sort,
              qipao_coin: this.form.qipao_coin,
              rmb: this.form.rmb,
              status: this.form.status,
              client_type: 1
            }
            editList(data).then(res => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.getList()
                this.dialogVisible = false
              }
            })
          }
        } else {
          return false
        }
      })
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false
    },
    handleClear() {
      this.$refs['form'].resetFields()
      this.form = {
        client_type: 1,
        sort: undefined,
        rmb: undefined,
        qipao_coin: undefined,
        status: 2
      }
    },
    //删除
    handleDel(row) {
      this.dataIds = { id: `${row.id}` }
      console.log(this.dataIds, 'id')
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      delList(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss">
.switchStyle .el-switch__label {
  position : absolute;
  display  : none;
  color    : #fff;
}

.switchStyle .el-switch__label--left {
  z-index : 9;
  left    : 18px;
}

.switchStyle .el-switch__label--right {
  z-index : 9;
  left    : -5px;
}

.switchStyle .el-switch__label.is-active {
  display : block;
}

.switchStyle.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width : 50px !important;
}
</style>
